from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
from  ..models import Order
from django.contrib.auth.decorators import permission_required


# @permission_required('myadmin.show_order',raise_exception = True)
def index(request):
    
    orderlist = Order.objects.all()
     # 获取搜索条件
    type = request.GET.get('type',None)
    kwords = request.GET.get('keywords',None)

    if type:
        # Q(x__contains= ),x必须是models定义好的且不能是外键，因为外键的值是对象，在这里不适合
        if type == 'all':
            from django.db.models import Q
            orderlist = Order.objects.filter(
                Q(addtime__contains=kwords)|
                Q(totalprice__contains=kwords)|
                Q(id__contains=kwords)   
            )
        elif type == "status":
            orderlist = Order.objects.filter(status__contains=kwords)
       
    else:

        orderlist = Order.objects.all()
    # 导入分页类
    from django.core.paginator import Paginator
    # 实例化分页对象,参数1,数据集合,参数2 每页显示条数
    paginator = Paginator(orderlist, 10)
    # 获取当前页码数
    p = request.GET.get('p',1)
    # 获取当前页的数据
    orderlist = paginator.page(p)

    for v in orderlist:
        for goods in v.orderinfo_set.all():
            print(goods)
            print(goods.goodsid.title)

    return render(request,'myadmin/order/list.html',{"orderlist":orderlist})
